<template>
  <div class="pb50">
    <el-form size="small" ref="formRef" :model="form" label-width="150px" v-if="!loading">
      <!--商品信息-->
      <Info :data="form"></Info>

      <!--规格-->
      <Spec :data="form"></Spec>

      <Other :data="form"></Other>

      <!--提交-->
    </el-form>

    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">取消</el-button>
      <el-button size="small" type="primary" @click="onSubmit" :loading="loading">提交</el-button>
    </div>
  </div>
</template>

<script>
import { reactive, toRefs, defineComponent } from "vue";
import { getProduct, addProduct } from "@/api/seckill";
import Info from "./part/Info.vue";
import Spec from "./part/Spec.vue";
import Other from "./part/Other.vue";

export default defineComponent({
  props: ['data'],
  components: {
    /*商品基本信息*/
    Info,
    /*规格*/
    Spec,
    /*其它*/
    Other,
  },
  setup() {
    const state = reactive({
      /*是否正在加载*/
      loading: true,
      /*表单*/
      form: {
        /*商品ID*/
        product_id: 0,
        /*活动ID*/
        seckill_activity_id: 0,
        /*商品状态*/
        status: 10,
        /*商品名称*/
        product_name: "",
        /*规格*/
        product_sku_id: 0,
        /*商品表格*/
        tableData: [],
        /*活动名称*/
        active_name: "",
        limit_num: "",
      },
    })
    const getData = ()=>{
      getProduct(
        {
          product_id: state.form.product_id,
          seckill_activity_id: state.form.seckill_activity_id,
        },
        true
      ).then((res) => {
        Object.assign(state.form, res.data.model);
        state.form.active_name = res.data.active.title;
        creatSpec();
        state.loading = false;
      });
    }
    const creatSpec = ()=>{
      let obj = {
        product_name: state.form.product_name,
        product_id: state.form.product_id,
        product_price: state.form.product_price,
        stock_num: state.form.product_stock,
        product_sku_id: state.form.product_sku.product_sku_id,
        spec_type: state.form.spec_type,
        seckill_price: 0,
        seckill_stock: 0,
        tempProduct: true,
        seckill_product_id: 0,
        seckill_product_sku_id: 0,
      };
      state.form.tableData.push(obj);
    }
    return {
      ...toRefs(state),
      getData,
      creatSpec,
    };
  },
  mounted(){
    if (this.$route.query.product_id != null) {
      this.form.product_id = this.$route.query.product_id;
      this.form.seckill_activity_id = this.$route.query.seckill_activity_id;
      this.getData();
    }
  },
  methods:{
    tableFormet(list) {
      list.forEach((item) => {
        item.product_attr = item.spec_name != null ? item.spec_name : "";
      });
      return list;
    },
    cancelFunc() {
      this.$router.back(-1);
    },
    onSubmit() {
      let self = this;
      self.$refs.formRef.validate((valid) => {
        if (valid) {
          let params = {
            product: {},
          };
          params.product_id = self.form.product_id;
          params.seckill_activity_id = self.form.seckill_activity_id;
          params.limit_num = self.form.limit_num;
          params.product.spec_list = self.tableFormet(self.form.tableData);
          self.loading = true;
          addProduct(params, true)
            .then((data) => {
              self.loading = false;
              ElMessage({
                message: "恭喜你，添加成功",
                type: "success",
              });
              self.cancelFunc();
            })
            .catch((error) => {
              self.loading = false;
            });
        }
      });
    },
  }
})
</script>

<style></style>
